#1.字符串解析与排序
"""word='''awfesdafhjkcasadckjsdajsadvcnksausafdschfsadfdsaasdfsdacsafsdaascsaasfdasdfsdasfadfsdafsadfjtyurjryjghnkuitki'''
list1=list(word)

while '\n'in list1:
    list1.remove('\n')

list2=sorted(list1)#排序
list3=list(set(list1))#去重
list4=[]
list5=[]

for i in list3:
    j=list2.count(i)
    list4.append((i,j))#计算每个字母出现的个数

list4=sorted(list4,key=lambda x:x[1],reverse=True)#降序排序
print(list4)

list5=list4[:10]#前十
print(list5)"""



#2.简易购物车程序

products=[
    ('Iphone',5800),
    ('Mac Pro',9800),
    ('Watch',6800),
    ('Python',120),
    ('Coffee',30),
    ('Pen',3),
    ('Notebook',15),
]

shopping_list=[]
salary=input("请输入你的工资:")
if salary.isdigit():
    salary=int(salary)
    while True:
        for index,item in enumerate(products):
            print(index,item)#枚举商品
        option=input("请选择您要购买的商品：")

        if option.isdigit():
            option=int(option)
            if 0<=option<len(products):
                option_product=products[option]
                if option_product[1]<=salary:
                    shopping_list.append(option_product)
                    salary-=option_product[1]
                    print("您选择的%s已加入购物车,您的余额为\033[31;1m%s\033[0m"%(option_product,salary))
                else:
                    print("\033[41;1m您的当前余额为%s,余额不足！\033[0m"%salary)
            else:
                print("抱歉，您选择的商品不存在！")
        elif option=='q':
            print("---------------shopping list------------------")
            for p in shopping_list:
                print(p)
            print("您的余额为:%s"%salary)
            exit()
        else:
            print("您的选择不合法！")
    else:
        print("您的工资输入不正确！")